package woa.linkedlist;

/**
 * 设计链表，双向链表
 *
 * @author wangpeng
 * @date 2021/11/21
 */
public class MyLinkedList1 {

    private Node head;

    private Node tail;

    private int count;

    public MyLinkedList1() {

    }

    public class Node {

        public int val;
        public Node prev;
        public Node next;

        public Node() {
        }

        public Node(int val) {
            this.val = val;
        }

        public Node(int val, Node next) {
            this.val = val;
            this.next = next;
        }

        public Node(int val, Node prev, Node next) {
            this.val = val;
            this.prev = prev;
            this.next = next;
        }

    }

    public int get(int index) {
        if (count == 0 || index > count - 1 || index < 0) {
            return -1;
        }
        Node cur = head;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return cur.val;
    }

    public void addAtHead(int val) {
        Node cur = new Node(val);
        if (count == 0) {
            head = tail = cur;
        } else {
            cur.next = head;
            head.prev = cur;
            head = cur;
        }
        count++;
    }

    public void addAtTail(int val) {
        Node cur = new Node(val);
        if (count == 0) {
            head = tail = cur;
        } else {
            tail.next = cur;
            cur.prev = tail;
            tail = cur;
        }
        count++;
    }

    public void addAtIndex(int index, int val) {
        if (index > count) {
            return;
        }
        if (index <= 0) {
            addAtHead(val);
            return;
        }
        if (index == count) {
            addAtTail(val);
            return;
        }
        Node cur = head;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        Node newNode = new Node(val);
        newNode.prev = cur.prev;
        newNode.next = cur;
        cur.prev.next = newNode;
        cur.prev = newNode;

        count++;
    }

    public void deleteAtIndex(int index) {
        if (count == 0 || index > count - 1 || index < 0) {
            return;
        }
        if (index == 0) {
            head = head.next;
            if (head != null) {
                head.prev = null;
            }
            if (index == count - 1) {
                tail = head;
            }
        } else {
            Node cur = head;
            for (int i = 0; i < index; i++) {
                cur = cur.next;
            }
            cur.prev.next = cur.next;
            if (cur.next != null) {
                cur.next.prev = cur.prev;
            }

            if (index == count - 1) {
                tail = cur.prev;
            }
        }
        count--;
    }

    public static void main(String[] args) {
        MyLinkedList1 linkedList = new MyLinkedList1();
        linkedList.addAtHead(7);
        linkedList.addAtTail(0);
        linkedList.deleteAtIndex(1);
        linkedList.addAtTail(5);
        linkedList.addAtIndex(1,1);
        linkedList.addAtIndex(2,6);
        linkedList.deleteAtIndex(2);
        linkedList.deleteAtIndex(1);
        linkedList.addAtTail(7);
        linkedList.addAtIndex(1,7);
        linkedList.addAtTail(6);
    }
}
